$(function() {
	ProjectLibrary = 1;
	initProjectLibraryData();
})
$(window).load(function(){
	console.log(500)
})

function initProjectLibraryData() {
	//PLcont-top
	getPLconttopData()
	//任务号-应收金额/到款金额
	getrwhyingshouMoneyChart()
	//八大成本占比
	getbadachengbenChart()
	//任务号-实际/计划支出金额
	getrwhshijiMoneyChart()
	//部门-实际/计划支出金额
	getbmshijiMoneyChart()
	//年度实际支出
	getnianduSJChart()
	//年度-EMIS计划完成数/计划数
	getnianduEMISChart()
	//年度-结算收入/结算成本
	getnianduJSChart()
	//
	getTbaleData()
	//明细表
	// getmingxiTable()
	//
	// getmaskChatrts()
}

function getPLconttopData() {
	var obj = {
		username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
		xm01: localStorage.getItem('rwh') ? localStorage.getItem('rwh') : '',
		status: 2,
		chartPage: 80801,
		chartNo: 1
	}
	ApiGet("getdata", obj, function(e) {
		var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
		let data = json.items[0].charts[0].data;
		data[0].key03 = parseFloat(data[0].key03)
		data[0].key06 = parseFloat(data[0].key06)
		data[0].key09 = parseFloat(data[0].key09)
		console.log(data)

		if (data.length > 0) {
			let options = ''
			options += ` <div class="shouru">
      <p>收入</p>
      <div class="detail">
        <div>
          <p>实际收入</p>
          <div> <span>${pricefmt(data[0].key02)}</span> 万元</div>
        </div>
        <div>
          <p>计划收入</p>
          <div> <span>${pricefmt(data[0].key01)}</span> 万元</div>
        </div>
        <div class="rate">
          <div>
            <p>完成率</p>
            <div> <span>${data[0].key03} %</span> </div>
          </div>
          <div class="pie">
            <svg><circle cx="20" cy="20" r="16" stroke="rgba(255,255,255,0.4)"  stroke-width="8" fill="none" stroke-linecap="round" />
              <circle cx="20" cy="20" r="16"  stroke="#FFFFFF" stroke-width="8" fill="none" stroke-dasharray="${Math.PI * 32}" stroke-dashoffset="${Math.PI * 32 * ((100 - data[0].key03) < 0 ? 0 : (100 - data[0].key03) / 100)}"/></svg>
          </div>
        </div>
      </div>
    </div>
    <div class="zhichu">
      <p>支出</p>
      <div class="detail">
        <div>
          <p>实际支出</p>
          <div> <span>${pricefmt(data[0].key05)}</span> 万元</div>
        </div>
        <div>
          <p>计划支出</p>
          <div> <span>${pricefmt(data[0].key04)}</span> 万元</div>
        </div>
        <div class="rate">
          <div>
            <p>完成率</p>
            <div> <span>${data[0].key06} %</span> </div>
          </div>
          <div class="pie">
          <svg>
          <circle cx="20" cy="20" r="16" stroke="rgba(255,255,255,0.4)"  stroke-width="8" fill="none" stroke-linecap="round" />
          <circle cx="20" cy="20" r="16"  stroke="${data[0].key06 > 100 ? '#F53F3F' : '#FFFFFF'}" stroke-width="8" fill="none" stroke-dasharray="${Math.PI * 32}" stroke-dashoffset="${Math.PI * 32 * ((100 - data[0].key06) < 0 ? 0 : (100 - data[0].key06) / 100)}" /></svg>
          </div>
        </div>
      </div>
    </div>
    <div class="EMIS">
      <p>EMIS</p>
      <div class="detail">
        <div>
          <p>计划完成数</p>
          <div> <span>${pricefmt(data[0].key08)}</span> 个</div>
        </div>
        <div>
          <p>计划总数</p>
          <div> <span>${pricefmt(data[0].key07)}</span> 个</div>
        </div>
        <div class="rate">
          <div>
            <p>完成率</p>
            <div> <span>${data[0].key09} %</span> </div>
          </div>
          <div class="pie">
          <svg><circle cx="20" cy="20" r="16" stroke="rgba(255,255,255,0.4)"  stroke-width="8" fill="none" stroke-linecap="round" />
          <circle cx="20" cy="20" r="16"  stroke="${data[0].key09 > 100 ? '#F53F3F':'#FFFFFF'}" stroke-width="8" fill="none" stroke-dasharray="${Math.PI * 32}" stroke-dashoffset="${Math.PI * 32 * ((100 - data[0].key09) < 0 ? 0 : (100 - data[0].key09) / 100)}"/></svg>
          </div>
        </div>
      </div>
    </div>`
			$('.PLcont-top').html(options)
		}
	});
}

function getrwhyingshouMoneyChart() {
	var obj = {
		username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
		xm01: localStorage.getItem('rwh') ? localStorage.getItem('rwh') : '',
		status: 2,
		chartPage: 80801,
		chartNo: 2
	}
	ApiGet("getdata", obj, function(e) {
		var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
		let data = json.items[0].charts[0].data;
		console.log(data)
		let legendData = ['应收', '到款']
		let yData = []
		let YSData = [];
		let DKData = [];
		if (data.length > 0) {
			data.forEach((item) => {
				yData.push(item.key01);
				YSData.push(item.key02);
				DKData.push(item.key03);
			})
		}

		let seriesData = [{
				name: '应收',
				type: 'bar',
				data: YSData,
				barGap: 0,
				itemStyle: {
					color: "#165DFF",
				}
			},
			{
				name: '到款',
				type: 'bar',
				data: DKData,
				barGap: 0,
				itemStyle: {
					color: "#14C9C9",
				}
			}
		]
		have('rwhyingshouMoney') ? BMHT('rwhyingshouMoney', seriesData, legendData, yData) : ''
	})
}

function getbadachengbenChart() {
	var obj = {
		username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
		xm01: localStorage.getItem('rwh') ? localStorage.getItem('rwh') : '',
		status: 2,
		chartPage: 80801,
		chartNo: 8
	}
	ApiGet("getdata", obj, function(e) {
		var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
		let data = json.items[0].charts[0].data;
		console.log(data)
		let chart = echarts.init(document.getElementById("badachengben"));
		chart.clear();
		let datas = [
			// { name: '原材料成本', value: 600,},
			// { name: '外部协作成本', value: 600,},
			// { name: '直接人工成本', value: 800,},
			// { name: '测试成本', value: 800,},
			// { name: '工装成本', value: 800,},
		]
		let legend = ''
		data.forEach((item) => {
			legend += `<div>
      <div class="item-list">
        <div class="item"> ${item.key01} </div>
        <div class="item-num">${item.key03} </div>
      </div>
    </div>`
			$('.legend').html(legend)

			datas.push({
				name: item.key01,
				value: item.key02,
				pre: item.key03
			})
		})
		let color = ["#165DFF", "#50CD89", "#722ED1", "#FD7E14", "#0DCAF0", "#479F76", "#F5319D", "#65789B"]
		document.querySelectorAll(".item")[0].style.borderColor = color[0]
		document.querySelectorAll(".item")[1].style.borderColor = color[1]
		document.querySelectorAll(".item")[2].style.borderColor = color[2]
		document.querySelectorAll(".item")[3].style.borderColor = color[3]
		document.querySelectorAll(".item")[4].style.borderColor = color[4]
		document.querySelectorAll(".item")[5].style.borderColor = color[5]
		document.querySelectorAll(".item")[6].style.borderColor = color[6]
		document.querySelectorAll(".item")[7].style.borderColor = color[7]

		let option = {
			tooltip: {
				trigger: 'item',
				axisPointer: {
					lineStyle: {
						type: "dashed",
						color: "#C9CDD4",
					},
				},
				backgroundColor: "transparent",
				borderWidth: 0,
				padding: 0,
				formatter: function(params) {
					let result = `
              <div style="width:250px;padding: 8px;
              border-radius: 6px;
              border:1px solid #FFFFFF ;
              box-shadow: 0 2px 5px 1px rgba(199, 211, 233, 0.83);
              background: -moz-linear-gradient(top, #F4F7FC 0%, #ffffff 100%);
              background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#F4F7FC), color-stop(100%,#ffffff));
              background: -webkit-linear-gradient(top, #F4F7FC 0%,#ffffff 100%);
              background: -o-linear-gradient(top, #F4F7FC 0%,#ffffff 100%);
              background: -ms-linear-gradient(top, #F4F7FC 0%,#ffffff 100%);
              background: linear-gradient(to bottom, #F4F7FC 0%,#ffffff 100%);">
               <p style="color: #1D2129;font-size: 12px;font-weight: 600; margin:0">${params.data.name}</p>
          <div style="display: flex; flex-direction: row;justify-content: space-between;
          background: #FFFFFF;border-radius: 4px;box-shadow: 6px 0 20px 1px rgba(34, 87, 188, 0.1);padding:6px 8px; margin-top: 4px;" >
            <div style="display: flex; flex-direction: row;align-items: center;">
              ${params.marker}
              <p style="color: #4E5969;font-size: 12px; margin:0">${params.name}</p>
            </div>
            <div class="">
              <h4 style="color: #1D2129;font-size: 14px;font-weight: 600; margin:0">${figure(params.value)}<span style="font-weight: normal;color: #86909C;font-size: 12px;">万元</span></h4>
            </div>
          </div>
            </div>`;
					return result;

				}
			},
			color: ["#165DFF", "#50CD89", "#722ED1", "#FD7E14", "#0DCAF0", "#479F76", "#F5319D", "#65789B"],
			series: [{
				type: 'pie',
				radius: '75%',
				avoidLabelOverlap: false,

				center: ['45%', '50%'],
				data: datas,
				itemStyle: {
					borderColor: '#fff',
					borderWidth: 1
				},
				label: { //不显示指示线
					normal: {
						show: false,
					}
				},
			}, ]
		}
		chart.setOption(option);
		window.addEventListener("resize", function() {
			chart.resize();
		});
		$("div").resize(function() {
			chart.resize();
		});

	});
}

function getrwhshijiMoneyChart() {
	var obj = {
		username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
		xm01: localStorage.getItem('rwh') ? localStorage.getItem('rwh') : '',
		status: 2,
		chartPage: 80801,
		chartNo: 3
	}
	ApiGet("getdata", obj, function(e) {
		var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
		let data = json.items[0].charts[0].data;
		console.log(data)
		let legendData = ['实际支出', '计划支出']
		let yData = []
		let SJData = [];
		let JHData = [];
		data.forEach((item) => {
			yData.push(item.key01);
			SJData.push(item.key03);
			JHData.push(item.key02);
		})
		let seriesData = [{
				name: '实际支出',
				type: 'bar',
				data: SJData,
				barGap: 0,
				itemStyle: {
					color: "#F7BA1E",
				}
			},
			{
				name: '计划支出',
				type: 'bar',
				data: JHData,
				barGap: 0,
				itemStyle: {
					color: "#FDF4BF",
				}
			}
		]
		have('rwhshijiMoney') ? BMHT('rwhshijiMoney', seriesData, legendData, yData) : ''
	})
}

function getbmshijiMoneyChart() {
	var obj = {
		username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
		xm01: localStorage.getItem('rwh') ? localStorage.getItem('rwh') : '',
		status: 2,
		chartPage: 80801,
		chartNo: 9
	}
	ApiGet("getdata", obj, function(e) {
		var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
		let data = json.items[0].charts[0].data;
		console.log(data)
		let legendData = ['实际支出', '计划支出']
		let xDanwei = '年';
		let yData = []
		let SJData = [];
		let JHData = [];
		data.forEach((item) => {
			yData.push(item.key01);
			SJData.push(item.key03);
			JHData.push(item.key02);
		})
		let seriesData = [{
				name: '实际支出',
				type: 'bar',
				data: SJData,
				barGap: 0,
				itemStyle: {
					color: "#F7BA1E",
				}
			},
			{
				name: '计划支出',
				type: 'bar',
				data: JHData,
				barGap: 0,
				itemStyle: {
					color: "#FDF4BF",
				}
			}
		]
		have('bmshijiMoney') ? BMHT('bmshijiMoney', seriesData, legendData, yData, xDanwei) : ''
	})
}

function getnianduSJChart() {
	var obj = {
		username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
		xm01: localStorage.getItem('rwh') ? localStorage.getItem('rwh') : '',
		status: 2,
		chartPage: 80801,
		chartNo: 4
	}
	ApiGet("getdata", obj, function(e) {
		var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
		let data = json.items[0].charts[0].data;
		console.log(data)

		let xData = [];
		let xDanwei = '年';
		let legendData = '年度实际支出'
		let SJDate = [];
		for (let i = 0; i < data.length; i++) {
			xData.push(data[i].key01)
			SJDate.push(data[i].key02)
		}
		let seriesData = [{
			name: '年度实际支出',
			smooth: false, //平滑曲线显示
			symbol: 'circle', //标记的图形为实心圆
			symbolSize: 8, //标记的大小
			itemStyle: {
				normal: {
					color: '#F7BA1E',
				},
			},
			showSymbol: false,
			lineStyle: {
				width: 2
			},
			type: 'line',
			areaStyle: {
				color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
						offset: 0,
						color: 'rgba(247, 186, 30, 0)'
					},
					{
						offset: 1,
						color: 'rgba(247, 186, 30, 0.20)'
					}
				])
			},
			data: SJDate
		}]
		have("nianduSJ") ? CBZEYD("nianduSJ", seriesData, legendData, xData, xDanwei) : "";
	})
}

function getnianduEMISChart() {
	let JHWCData = [120, 390, 400, 500, 800, 500];
	let JHData = [200, 400, 780, 900, 730, 500];
	let legendData = ['计划完成数', '计划数'];
	let xData = ['2018', '2019', '2020', '2021', '2022', '2023'];
	let seriesData = [{
			name: "计划完成数",
			barWidth: "16%",
			type: "bar",
			data: JHWCData,
			barGap: 0,
			itemStyle: {
				color: "#722ED1",
			}
		},
		{
			name: "计划数",
			barWidth: "16%",
			type: "bar",
			barGap: 0,
			data: JHData,
			itemStyle: {
				color: "#DDBEF6",
			}
		}
	];
	have("nianduEMIS") ? bmReceived("nianduEMIS", seriesData, legendData, xData, '', '', '个') : "";
}

function getnianduJSChart() {
	let JHWCData = [120, 390, 400, 500, 800, 500];
	let JHData = [200, 400, 780, 900, 730, 500];
	let legendData = ['结算收入', '结算成本'];
	let xData = ['2018', '2019', '2020', '2021', '2022', '2023'];
	let seriesData = [{
			name: "结算收入",
			barWidth: "16%",
			type: "bar",
			data: JHWCData,
			barGap: 0,
			itemStyle: {
				color: "#0FC6C2",
			}
		},
		{
			name: "结算成本",
			barWidth: "16%",
			type: "bar",
			barGap: 0,
			data: JHData,
			itemStyle: {
				color: "#F7BA1E",
			}
		}
	];
	have("nianduJS") ? bmReceived("nianduJS", seriesData, legendData, xData, '', '', '万元') : "";
}
let page = 1;
let limit = 10;

function getTbaleData() {
	var obj = {
		username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
		xm01: localStorage.getItem('rwh') ? localStorage.getItem('rwh') : '',
		status: 2,
		chartPage: 80801,
		chartNo: 5,
		pageNum: page,
		pageRowCount: limit
	}
	ApiGet("getdata", obj, function(e) {
		var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
		let data = json.items[0].charts[0].data;
		console.log(data)
		getmingxiTable(data)
	})
}
// let tableHeight = 470
function getmingxiTable(Tabledata) {
	console.log(Tabledata, '明细表')
	let data = Tabledata
	let cols = [
		[{
				field: 'key01',
				title: '任务号',
				width: '6%',
				templet: function(d) {
					return `
          <div class="rwh">
            <span>${d.key01}</span> 
          </div>
        `
				}
			},
			{
				field: 'key02',
				title: '型号',
				width: '6%',
			},
			{
				field: 'key03',
				title: '阶段',
				width: '80',
			},
			{
				field: 'key04',
				title: '部段',
				width: '80',
			},
			{
				field: 'key05',
				title: '甲方单位',
				width: '15%',
			},
			{
				field: 'key06',
				title: '业务部门',
				width: '8%',
			},
			{
				field: 'key07',
				title: '计划收入',
				align: 'right',
				width: '8%',
				templet: function(d) {
					return `
          <div class="rwh">
            <span>${d.key07 ? pricefmt(d.key07) :0}</span> 
          </div>
        `
				}
			},
			{
				field: 'key08',
				align: 'right',
				title: '实际收入',
				width: '8%',
				templet: function(d) {
					return `
          <a class="diff" onclick="SRtanchu(this)" name=${d.key16}>
            <div>
              <span>${ d.key08 ? pricefmt(d.key08) :0}</span> 
            </div>
          </a>
        `
				}
			},
			{
				field: 'key09',
				align: 'right',
				title: '计划支出',
				width: '8%',
				templet: function(d) {
					return `
          <div class="rwh">
            <span>${d.key09 ? pricefmt(d.key09) :0}</span> 
          </div>
        `
				}
			},
			{
				field: 'key10',
				align: 'right',
				title: '实际支出',
				width: '8%',
				templet: function(d) {
					return `
          <a class="diff" onclick="ZCtanchu(this)" name=${d.key16}>
            <div>
              <span>${ d.key10 ? pricefmt(d.key10) :0}</span> 
            </div>
          </a>
        `
				}
			},
			{
				field: 'key11',
				align: 'right',
				title: 'EMS计划总数',
				width: '8%',
				templet: function(d) {
					return `
          <div class="rwh">
            <span>${d.key11 ? pricefmt(d.key11) :0}</span> 
          </div>
        `
				}
			},
			{
				field: 'key12',
				align: 'right',
				title: 'EMS计划完成数',
				width: '8%',
				templet: function(d) {
					return `
          <div class="rwh">
            <span>${d.key12 ? pricefmt(d.key12) :0}</span> 
          </div>
        `
				}
			},
			{
				field: 'key13',
				align: 'right',
				title: '结算收入',
				width: '8%',
				templet: function(d) {
					return `
          <div class="rwh">
            <span>${d.key13 ? pricefmt(d.key13) : 0}</span> 
          </div>
        `
				}
			},
			{
				field: 'key14',
				align: 'right',
				title: '结算成本',
				width: '8%',
				templet: function(d) {
					return `
          <div class="rwh">
            <span>${d.key14 ? pricefmt(d.key14) : 0}</span> 
          </div>
        `
				}
			},
		],
	];
	tableDrawing(data) // 数据传到 table组件
	pagesDrawing(data) // 数据传到 分页组件
	//表内容
	function tableDrawing(data) {
		layui.use('table', function() {
			let table = layui.table;
			table.render({
				elem: '#newsList',
				cols: cols,
				data: data, // 数据
				limit: limit, // 每页显示的条数
				// height: tableHeight,
				// even: true, //开启隔行背景
				done: function(res, curr, count) {
					var that = this.elem.next();
					res.data.forEach(function(item, index) {
						if (index % 2 == 0) {
							var tr = that.find(".layui-table-box tbody tr[data-index= " +
								index + "]").css("background-color", "#F3F7FF");
						} else {
							var tr = that.find(".layui-table-box tbody tr[data-index= " +
								index + "]").css("background-color", "#FFF");
						}
					})
				}
			});
		})
	}
	//表脚
	function pagesDrawing(data) {
		let totolPage = ''
		if (data.length > 0) {
			totolPage = data[0].key15 ? data[0].key15 : 0
		} else {
			totolPage = 0
		}
		layui.use('laypage', function() {
			let laypage = layui.laypage;
			laypage.render({
				elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
				count: totolPage, //数据总数，从服务端得到
				theme: '#E8F3FF',
				limit: limit, // 每页条数
				prev: '<em><</em>',
				next: '<em>></em>',
				layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
				jump: function(obj, first) {
					console.log(obj, '第几页 ')
					console.log(obj.curr, '第几页 ')
					//首次不执行
					if (!first) {
						limit = obj.limit;
						numpage(obj.curr, obj.limit) // 分页点击传参 
					}
				}
			});
		})
	};
	//分页
	function numpage(curr, page_count) { // 分页切换-数据请求
		console.log(curr, page_count)
		var obj = {
			username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
			xm01: localStorage.getItem('rwh') ? localStorage.getItem('rwh') : '',
			status: 2,
			chartPage: 80801,
			chartNo: 5,
			pageNum: curr, //页号
			pageRowCount: page_count, //每页多少条
		};
		ApiGet("getdata", obj, function(e) {
			var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
			var chartData = json.items[0].charts;
			let data = chartData[0].data
			console.log(data, '分页之后的数据')
			tableDrawing(data);
		});

	};
}
//弹框表格
function getmaskChatrts(Tabledata, leibie, rwhcode) {
	console.log(Tabledata, leibie, '明细表', '类别（支出/收入）')
	let LB = leibie //类别
	let RWHcode = rwhcode
	let data = Tabledata
	// data = Tabledata
	let cols = [
		[{
				field: 'key02',
				title: '年度',
				templet: function(d) {
					return `
          <div class="rwh">
            <span>${d.key02}</span> 
          </div>
        `
				}
			},
			{
				field: 'key03',
				title: '费用类别',
			},
			{
				field: 'key04',
				title: '费用名称',
			},
			{
				field: 'key05',
				title: '金额',
			},
		],
	];
	tableDrawing(data) // 数据传到 table组件
	pagesDrawing(data) // 数据传到 分页组件

	//表内容
	function tableDrawing(data) {
		layui.use('table', function() {
			let table = layui.table;
			table.render({
				elem: '#newsListMask',
				cols: cols,
				data: data, // 数据
				limit: limit, // 每页显示的条数
				// height: tableHeight,
				// even: true, //开启隔行背景
				done: function(res, curr, count) {
					var that = this.elem.next();
					res.data.forEach(function(item, index) {
						if (index % 2 == 0) {
							var tr = that.find(".layui-table-box tbody tr[data-index= " +
								index + "]").css("background-color", "#F3F7FF");
						} else {
							var tr = that.find(".layui-table-box tbody tr[data-index= " +
								index + "]").css("background-color", "#FFF");
						}
					})
				}
			});
		})
	}
	//表脚
	function pagesDrawing(data) {
		let totolPage = ''
		if (data.length > 0) {
			totolPage = data[0].key06 ? data[0].key06 : 0
		} else {
			totolPage = 0
		}
		layui.use('laypage', function() {
			let laypage = layui.laypage;
			laypage.render({
				elem: 'laypageMask', //注意，这里的 page 是 ID，不用加 # 号
				count: totolPage, //数据总数，从服务端得到
				theme: '#E8F3FF',
				limit: limit, // 每页条数
				prev: '<em><</em>',
				next: '<em>></em>',
				layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
				jump: function(obj, first) {
					console.log(obj, '第几页 ')
					console.log(obj.curr, '第几页 ')
					//首次不执行
					if (!first) {
						limit = obj.limit;
						numpage(obj.curr, obj.limit) // 分页点击传参 
					}
				}
			});
		})
	};
	//分页
	function numpage(curr, page_count) { // 分页切换-数据请求
		console.log(LB, RWHcode, '分页切换-数据请求')
		console.log(curr, page_count)
		var obj = {
			username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
			xm01: RWHcode,
			status: 2,
			chartPage: 80801,
			chartNo: LB == 0 ? 7 : 6,
			pageNum: curr, //页号
			pageRowCount: page_count, //每页多少条
		};
		ApiGet("getdata", obj, function(e) {
			var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
			var chartData = json.items[0].charts;
			let data = chartData[0].data
			console.log(data, '分页之后的数据')
			tableDrawing(data);
		});
	};
}
//实际收入数据
function SRtanchu(obj) {
	$("#maskName").text("实际收入")
	let rwhcode = obj.getAttribute("name")
	if (obj.getAttribute("name")) {
		var obj = {
			username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
			xm01: obj.getAttribute("name"),
			status: 2,
			chartPage: 80801,
			chartNo: 7,
			pageNum: page,
			pageRowCount: limit
		};
		ApiGet("getdata", obj, function(e) {
			var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
			var chartData = json.items[0].charts;
			let data = chartData[0].data
			console.log(data, '实际收入弹框--明细')
			// if (data.length > 0) {
			getmaskChatrts(data, 0, rwhcode)
			toggle()
			// }
			// else {
			// $("#mask-chatrts").html(`<div class="noData"> 暂无明细 ~ </div>`)
			// toggle()
			// }
		})
	}

}
//实际支出数据
function ZCtanchu(obj) {
	$("#maskName").text("实际支出")
	let rwhcode = obj.getAttribute("name")
	if (obj.getAttribute("name")) {
		var obj = {
			username: localStorage.getItem('user') ? localStorage.getItem('user') : '',
			xm01: obj.getAttribute("name"),
			status: 2,
			chartPage: 80801,
			chartNo: 6,
			pageNum: page,
			pageRowCount: limit
		};
		ApiGet("getdata", obj, function(e) {
			var json = JSON.parse(e.replace(/[\r|\n|\t]/g, ""));
			var chartData = json.items[0].charts;
			let data = chartData[0].data
			console.log(data, '实际支出弹框--明细')
			// if (data.length > 0) {
			getmaskChatrts(data, 1, rwhcode)
			toggle()
			// }
			// else {
			//   $("#mask-chatrts").html(`<div class="noData"> 暂无明细 ~ </div>`)
			//   toggle()
			// }
		})
	}
}

function toggle() {
	document.querySelector('.All-IncomingPayment-Mask').style.display = document.querySelector(
		'.All-IncomingPayment-Mask').style.display !== 'block' ? 'block' : 'none'
}
//关闭弹窗 
function MaskClose() {
	console.log('mask关闭')
	toggle()
}






$(function() {
	$('.search>div.section2 ').on('click', '.options div', function(event) {
		event.stopPropagation();
		let options = ''
		let num = '';
		fullRWH = []
		fullRWHText = '';
		$(this).toggleClass('checked')
		if ($(this).hasClass('all')) {
			if ($(this).hasClass('checked')) {
				$(this).removeClass('half')
				$('.search>div.section2 .options div.unit').addClass('checked');
				$('.search>div.section2 .sector div').html('<span>全部</span>')
				fullRWH = localStorage.getItem('newArrfullRWH')
				fullRWHText = ''
			} else {
				$(this).removeClass('half')
				$('.search>div.section2 .options div.unit').removeClass('checked')
				fullRWH = ''
				fullRWHText = ''
			}
		} else {
			if ($('.search>div.section2 .options div.unit.checked').length == 0) {
				$('.search>div.section2 .options div.all').removeClass('checked')
				$('.search>div.section2 .options div.all').removeClass('half')
				$('.search>div.section2 .sector div').html('')
				fullRWH = ''
				fullRWHText = ''
			} else if ($('.search>div.section2 .options div.unit.checked').length < $(
					'.search>div.section2 .options div.unit').length || !$(
					'.search>div.section2 .options div').eq(0).hasClass('all')) {
				$('.search>div.section2 .options div.all').removeClass('checked')
				$('.search>div.section2 .options div.all').addClass('half')
				$('.search>div.section2 .options div.unit.checked').each(function(index) {
					// console.log($(this))
					fullRWH.push($(this).attr('data-num'))
					fullRWHText += $(this).find('p').text() + ' '
					if (index < 3) {
						let op = `<span>${$(this).find('p').text()}</span>`;
						options += op
					} else {
						num = `<span>+${index - 2}</span>`;
					}

				})
				if (num) {
					options += num
				}
				fullRWH = fullRWH.join(',')
				$('.search>div.section2 .sector div').html(options)
			} else if ($('.search>div.section2 .options div.unit.checked').length == $(
					'.search>div.section2 .options div.unit').length) {
				$('.search>div.section2 .options div.all').addClass('checked')
				$('.search>div.section2 .options div.all').removeClass('half')
				if ($('.search>div.section2 .options div').hasClass('all')) {
					$('.search>div.section2 .sector div').html('<span>全部</span>')
				}
				fullRWH = ''
				fullRWHText = ''
			}
		}
		fullRWHText = fullRWHText.trim()
		localStorage.setItem('rwh', fullRWH);
		localStorage.setItem('rwhText', fullRWHText)
		change()

	})

	$('.search>div.section2').click(function() {
		$('.search>div.section2 .options').toggle()
	})
	$('.search>div.section2').click(function(event) {
		event.stopPropagation();
	})
	$(document).click(function() {
		$('.search>div.section2 .options').hide()
	})

	document.getElementsByClassName('options')[0] ? getDepartment() : ''


	function getDepartment() {
		let fullRWH = []
		let options = ''
		$.get(`${API_URL}getList/xm`, {}, function(e) {
			console.log(e.items)
			e.items.unshift({
				bh: '',
				rwh: '全部'
			})
			for (let i = 0; i < e.items.length; i++) {
				fullRWH.push(e.items[i].bh)
				options += `<div class="${e.items[i].rwh == '全部'?'all':'unit'}" data-num="${e.items[i].bh}">
                      <i></i>
                      <p>${e.items[i].rwh}</p>
                    </div>`
			}
			console.log(localStorage.getItem('rwhText'))
			$('.search>div.section2 .sector span').text(e.items[0].rwh)
			$('.search>div.section2 .options').html(options)
			//全部任务号
			const newArrfullRWH = fullRWH.filter(item => item !== '')
			localStorage.setItem('newArrfullRWH', newArrfullRWH);
			if (!localStorage.getItem('rwhText')) {
				if (e.items[0].rwh == '全部') {
					$('.search>div.section2 .options div').addClass('checked')
				} else {
					$('.search>div.section2 .options div').eq(0).addClass('checked')
				}
				fullRWH = newArrfullRWH
				fullRWHText = e.items[0].rwh
				localStorage.setItem('rwh', fullRWH);
				localStorage.setItem('rwhText', fullRWHText)
			} else {
				if (localStorage.getItem('rwhText') != '全部') {
					initrwh(e.items)
				} else {
					$('.search>div.section2 .options div').addClass('checked')
				}
			}
		})
	}





	function initrwh(data) {
		let rwhText = localStorage.getItem('rwhText').split(' ');
		console.log(rwhText)
		for (let i = 0; i < data.length; i++) {
			for (let j = 0; j < rwhText.length; j++) {
				if (rwhText[j] == data[i].rwh) {
					$('.search>div.section2 .options div').eq(i).addClass('checked')
				}
			}
		}
		let spans = '';
		const nums = rwhText.length > 3 ? 3 : rwhText.length;
		for (let i = 0; i < nums; i++) {
			spans += `<span>${rwhText[i]}</span>`
		}
		if (rwhText.length > 3) {
			spans += `<span>+${rwhText.length - 3}</span>`
		}
		$('.search>div.section2 .options div.all').addClass('half')
		$('.search>div.section2 .sector div').html(spans)
	}
})
